import { Controller, Get, HttpStatus, Post, Query } from '@nestjs/common';
import { CommonService } from './common.service';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiQuery,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import { MessagePattern } from '@nestjs/microservices';
import { RequireLogin } from '@app/auth/utils/guard';
import { PublicVoError, PublicVoSuccess } from '@app/utils/publicVo';
@ApiTags('公共系统-基础模块')
@Controller()
export class CommonController {
  constructor(private readonly commonService: CommonService) {}

  /**
   * TCP 模块
   */
  @MessagePattern('post-upload')
  async getUploadTCP(data: any) {
    const { query } = data;
    return this.commonService.upload(query.name);
  }

  @MessagePattern('get-region')
  async getRegionTCP() {
    return await this.commonService.list();
  }

  @MessagePattern('get-short')
  async getShortNameTCP() {
    return await this.commonService.shortList();
  }

  @MessagePattern('get-compeny')
  getHelloTCP() {
    return this.commonService.getHello();
  }
  /**
   * HTTP 模块
   */
  @Post('upload')
  @ApiOperation({
    summary: '图片上传',
    description: '图片上传OSS预签名接口',
  })
  @ApiQuery({
    name: 'name',
    type: String,
    description: '图片名',
    required: true,
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  @ApiBearerAuth()
  @RequireLogin()
  @ApiOperation({ summary: '图片上传', description: '图片上传接口' })
  async getUpload(@Query('name') name: string) {
    return this.commonService.upload(name);
  }

  @Get('region')
  @ApiOperation({ summary: '省市区', description: '地区省市区三级联动接口' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async getRegion() {
    return await this.commonService.list();
  }

  @Get('short')
  @ApiOperation({
    summary: '省份/简称（车牌）/省会城市',
    description: '省份/简称（车牌）/省会城市接口',
  })
  @ApiResponse({
    status: HttpStatus.OK,
    description: '请求成功',
    type: PublicVoSuccess,
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: '请求失败',
    type: PublicVoError,
  })
  async getShortName() {
    return await this.commonService.shortList();
  }

  @Get('compeny')
  @ApiOperation({
    summary: '消息队列',
    description: 'rabbitMQ 消息队列 使用例子接口',
  })
  @ApiBearerAuth()
  @RequireLogin()
  getHello() {
    return this.commonService.getHello();
  }

  /**
   * gRPC 跨语言通信
   * 客户端 调用
   * @returns 调用gRPC传递数据
   */
  @Get('user/:id')
  @ApiOperation({
    summary: 'gRPC跨语言通信',
    description: 'gRPC 跨语言通信 使用例子接口',
  })
  @ApiBearerAuth()
  @RequireLogin()
  async getUser() {
    return await this.commonService.getUser(2); // 模拟获取 ID 为 1 的用户信息
  }
}
